package cn.community.system.web.controller;

import cn.community.system.domain.Activity;
import cn.community.system.domain.ActivityRecord;
import cn.community.system.domain.Association;
import cn.community.system.query.ActivityQuery;
import cn.community.system.service.IActivityRecordService;
import cn.community.system.service.IActivityService;
import cn.community.system.service.IAssociationService;
import cn.community.system.util.JsonResult;
import cn.community.system.util.UserContext;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private IActivityService activityService;

    @Autowired
    private IActivityRecordService activityRecordService;

    @Autowired
    private IAssociationService associationService;

    @RequestMapping("/admin/list")
    @RequiresPermissions(value = {"activity/admin/list","超管-活动管理列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") ActivityQuery qo){
        model.addAttribute("page",activityService.query(qo));
        return "activity/admin/list";
    }

    @RequestMapping("/association/list")
    @RequiresPermissions(value = {"activity/association/list","社团-活动管理列表"},logical = Logical.OR)
    public String associationList(Model model, @ModelAttribute("qo") ActivityQuery qo){
        model.addAttribute("page",activityService.queryAssociation(qo));
        return "activity/association/list";
    }

    @RequestMapping("/member/list")
    @RequiresPermissions(value = {"activity/member/list","社团成员-活动信息列表"},logical = Logical.OR)
    public String memberList(Model model, @ModelAttribute("qo") ActivityQuery qo){
        model.addAttribute("page",activityService.queryMember(qo));
        return "activity/member/list";
    }

    @RequestMapping("/member/detail")
    @RequiresPermissions(value = {"activity/member/detail","社团成员-活动详情页面"},logical = Logical.OR)
    public String detail(Model model, Long id){
        Activity activity = activityService.get(id);
        Association association = associationService.get(activity.getSponsorId());
        model.addAttribute("activity",activity);
        model.addAttribute("association",association);
        ActivityRecord record = activityRecordService.getByIds(id, UserContext.getCurrentMember().getId());
        int status = -1;
        if(new Date().getTime() > activity.getEndTime().getTime()){
            status = 2;  // 活动结束
        }else if(new Date().getTime() > activity.getApplyEndTime().getTime()){
            status = 1;  // 报名截止
        }else if(record != null){
            if(record.getStatus() == ActivityRecord.APPLYING){
                status = 0; // 报名中
            }
        }
        model.addAttribute("status",status);
        return "activity/member/detail";
    }

    @RequestMapping("/admin/input")
    @RequiresPermissions(value = {"activity/admin/input","超管-活动新增/编辑页面"},logical = Logical.OR)
    public String inputAdmin(Model model,Long id){
        if(id!=null){ //编辑
            Activity activity = activityService.get(id);
            model.addAttribute("activity",activity);
        }
        return "activity/admin/input";
    }

    @RequestMapping("/association/input")
    @RequiresPermissions(value = {"activity/association/input","社团-活动新增/编辑页面"},logical = Logical.OR)
    public String input(Model model,Long id){
        if(id!=null){ //编辑
            Activity activity = activityService.get(id);
            model.addAttribute("activity",activity);
        }
        return "activity/association/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"activity/saveOrUpdate","超管-活动新增/编辑功能"},logical = Logical.OR)
    public Object saveOrUpdate(Activity activity){
        if(activity.getId() == null){
            // 新增活动
            activityService.save(activity);
        }else {
            // 代表修改
            activityService.update(activity);
        }
        return JsonResult.success();
    }

    @RequestMapping("/saveOrUpdate2")
    @ResponseBody
    @RequiresPermissions(value = {"activity/saveOrUpdate2","社团-活动新增/编辑功能"},logical = Logical.OR)
    public Object saveOrUpdate2(Activity activity){
        if(activity.getId() == null){
            // 新增活动
            activityService.save2(activity);
        }else {
            // 代表修改
            activityService.update2(activity);
        }
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"activity/delete","活动信息删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        activityService.delete(id);
        return JsonResult.success();
    }

    @RequestMapping("/getSynopsisById")
    @ResponseBody
    public Object getSynopsisById(Long id){
        return JsonResult.success(activityService.get(id).getSynopsis());
    }

    @RequestMapping("/getDetailById")
    @ResponseBody
    public Object getDetailById(Long id){
        return JsonResult.success(activityService.get(id).getDetails());
    }

    // 活动审核
    @RequestMapping("/passOne")
    @ResponseBody
    @RequiresPermissions(value = {"activity/passOne","活动审核"},logical = Logical.OR)
    public JsonResult passOne(Integer hot,Long id){
        activityService.passOne(id,hot);
        return JsonResult.success();
    }

    // 查询主办方
    @RequestMapping("/listAssociation")
    @ResponseBody
    public Object listAssociation(){
        return JsonResult.success(associationService.listAll());
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public Object listAll(){
        return JsonResult.success(activityService.listAll());
    }
}
